// miniprogram/pre_loadModules/orderModule/pages/orderCheck/orderCheck.js
import { getdefaultAddress } from '@/miniprogram/pre_loadModules/utils/Memberaddress.js'
import { orderconfirm, orderSubmit, getpostAges, orderconfirmBuyNow, getExpressListFun } from '@/miniprogram/pre_loadModules/orderModule/API/order'
Page({

   /**
    * 页面的初始数据
    */
   data: {
      height: null, width: null, defaultAddress: null, orderInfo: null, disablesubmit: true,
      defaulttip: "请您设置收货地址！", noteMessage: "请输入留言信息",
      submitData: {},
      fromCar: false,
      isBuyNow: false, skuId: 0, count: 0, spuId: 0,postAge:0.0,
      postAgeData: {},
      expressList: {},
      dropOption: [],
      expressValue: 'shunfeng',
      orderSn:null
   },

   chooseAddressTap(e) {
      let { orgin } = e.currentTarget.dataset
      wx.redirectTo({
         url: `/miniprogram/pre_loadModules/orderModule/pages/address/address?orgin=${orgin}`,
      })
   },

   //订单提交
   orderSubmitBtn(e) {
      let { submitData, defaultAddress, noteMessage, orderInfo, fromCar, isBuyNow, skuId, count } = this.data
      submitData.addrId = defaultAddress.id
      submitData.payPrice = orderInfo.payPrice
      submitData.note = noteMessage
      submitData.orderToken = orderInfo.orderToken
      submitData.skuId = skuId
      if (isBuyNow) {
         submitData.isBuyNow = isBuyNow
         submitData.count = count
      }
      if (submitData.addrId == null || submitData.addrId == '') {
         wx.showToast({
            title: '请设置详细地址！',
            icon: 'error'
         })
         return
      }
      if (submitData.note == null) {
         wx.showToast({
            title: '请设置备注信息！',
            icon: 'error'
         })
         return
      }
      this.orderSubmit(submitData)
      this.setData({
         opsData: null
      })
   },

   async orderSubmit(data) {
      let res = await orderSubmit(data)
      if (res.data.code === 200) {
        let orderSn = res.data.data.order.orderSn
        let id = res.data.data.order.id
        console.log(orderSn);
         wx.redirectTo({
            url: `/miniprogram/pre_loadModules/orderModule/pages/orderPayment/orderPayment?orderSn=${orderSn}&id=${id}`,
          })
      }

   },

   /**
    * 生命周期函数--监听页面加载
    */

   onLoad(options) {
      let { isBuyNow, skuId, count, spuId, fromCar } = options
      this.setData({ isBuyNow, skuId, count, spuId, fromCar })
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow() {
      let { height, width } = this.data
      let data = {}
      height = wx.getWindowInfo().windowHeight
      width = wx.getWindowInfo().windowWidth
      height = (height - 100) + 'px'
      data.defaultStatus = 1
      this.getdefaultAddressFun(data)
      this.orderConfirmFun()
      this.getExpressList()
      this.setData({
         height, width
      })
   },
   async getpostAgesfun() {
      let that = this
      let { postAgeData,skuId, spuId, expressValue ,postAge,} = this.data
      let data = {}
      data.skuId = skuId
      data.spuId = spuId
      data.kuaidicom = expressValue
      let res = await getpostAges(data)
      if (res.data.code === 200) {
         postAgeData = res.data.data,
         postAge = postAgeData.postAge
         console.log("执行了Postage。。。"+postAge);
         that.setData({
            postAgeData,
            postAge
         })
      }
   },
   async orderConfirmFun() {
      let { fromCar, isBuyNow, count, skuId, orderInfo } = this.data
      this.getpostAgesfun()
      let opsData = {}
      let that = this
      let res = null
      opsData.skuId = skuId
      opsData.count = count
      opsData.isBuyNow = isBuyNow
      if (fromCar) {
         res = await orderconfirm()
      } else if (isBuyNow) {
         res = await orderconfirmBuyNow(opsData);
      }
      if (res.data != null && res.data.code === 200) {
         orderInfo = res.data.data
         orderInfo.payPrice += this.data.postAge
         that.setData({
            orderInfo
         })
      }
   },
   dropMenuchange(e) {
      this.getpostAgesfun()
   },

   async getdefaultAddressFun(data) {
      let { defaultAddress, postAgeData, expressValue } = this.data
      let res = await getdefaultAddress(data);
      if (res.data.code == 200) {
         defaultAddress = res.data.data
         if (defaultAddress != null) {
            let phone = defaultAddress.phone.replace(defaultAddress.phone.substring(3, 7,), '****')
            defaultAddress.phone = phone
            postAgeData.addrId = defaultAddress.id
            postAgeData.kuaidcom = expressValue
         }
      }
      // this.getpostAgesfun(postAgeData)
      this.setData({
         defaultAddress
      })
   },

   async getExpressList() {
      let { expressList, dropOption } = this.data
      let res = await getExpressListFun()
      if (res.data.code === 200) {
         expressList = res.data.data
         dropOption = expressList.map(item => {
            let o = {}
            o.text = item.companyName
            o.value = item.kuaidicom
            o.icon = ''
            return o
         })
         this.setData({
            expressList,
            dropOption
         })
      }
   },
   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide() {

   },


   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage() {

   }
})